// sharing/pages/enter/enter.js
var app = getApp();
Page({
	TOAPPID: require('../../resource/components/footer/footer.js'),
  /**
   * 页面的初始数据
   */
  data: {
	  openid: '',
	  show: false,
	  wifiOpen: false,
	  wifiList: [],
	  selWifi: '请选择WiFi',
	  photoType: 0,
	  res: app.siteInfo.resource,
	  showPic: false,
	  isAdv: false,
	  pkey: 0,
	  uploadPic: false,
	  DOWN: '',
  },
  onShow: function(){
	  var id = wx.getStorageSync('listId');
	  if(id){
		  this.onLoad();
	  }
  },
  onHide: function(){
	  if(this.data.wifi) this.setData({wifi: false, isAdv: false, uploadPic: false, advPic: false, pkey: 0, selWifi: '请选择WiFi'});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	var id = wx.getStorageSync('listId');
	var that = this;
	app.footerInit(that);
	wx.showLoading();
	app.getShareData(function(res){that.setData({cfg: res.data})});
	app.getUserInfo(function(openid){
		that.setData({openid: openid});
		wx.getUserInfo({
			success: function (wxInfo) {
				var info = JSON.parse(wxInfo.rawData);
				app.util.request({
					url: 'entry/wxapp/updateUser',
					data: { openid: openid, avatar: info.avatarUrl, nickname: info.nickName, gender: info.gender, m: 'junsion_wifinav'},
					cachetime: 0,
					success: function (res) {
						
					}
				});
			}
		});
		app.util.request({
			url: 'entry/wxapp/enter',
			data: { openid: openid, op: 'getPic', m: 'junsion_wifinav'},
			cachetime: 0,
			success: function (res) {
		        wx.setNavigationBarTitle({
		          title: res.data.title ? res.data.title : '入驻WIFI',
		        })
				wx.hideLoading();
				var data = res.data;
				that.setData({show: true});
				if(data.enter_1){
					that.setData({enter_1: data.enter_1});
				}
				if(data.enter_2){
					that.setData({enter_2: data.enter_2});
				}
				if(data.enter_3){
					that.setData({enter_3: data.enter_3});
				}
				if(data.packages){
					that.setData({packages: data.packages});
				}
			}
		});
	});
	//存在id则编辑入驻信息
	if(id){
		app.util.request({
			url: 'entry/wxapp/enter',
			data: { id: id, op: 'getWifi', m: 'junsion_wifinav'},
			cachetime: 0,
			success: function (res) {
		        if(res.data.code==1){
		        	wx.removeStorageSync('listId');
		        	that.setData({wifi: res.data.wifi});
		        	if(res.data.wifi.advPic){
        	            that.setData({isAdv: true, advPic: res.data.wifi.advPic, advHeight: 652 / res.data.wifi.width * res.data.wifi.height});
		        	}
		        }else{
		        	wx.showModal({
		        		title: '温馨提示',
		        		content: res.data.msg,
		        		showCancel: false,
		        		success: function(){
		        			wx.navigateTo({url: '../user/mylist/index'});
		        		}
		        	});
		        }
			}
		});
	}else{
		that.getWiFiList();
		wx.getSystemInfo({
			success: function(res){
				if(res.platform == 'ios'){
					that.setData({photoType: 1});
				}
				that.restart(1);
			}
		});
	}
  },
  restart: function(t){
	var that = this;
	var showPic = that.data.showPic;
	var photoType = that.data.photoType;
	if(t==1 && photoType==1){
		var result = app.getWiFiSto(that);
		if(result){
			that.setData({wifiOpen: true});
			return ;
		}
	}
	if(photoType==1 && !showPic){
		that.setData({showPic: true});
		return;
	}
	wx.showLoading();
	wx.getWifiList({
	  success: function(res){
		  if(res.errCode==0 || res.errMsg=='getWifiList:ok'){
			  that.setData({wifiOpen: true});
		  }else{
			  that.setData({wifiOpen: false});
			  that.setData({wifiList:[],selWifi: '请选择WiFi'});
		  }
	  },
	  fail: function(r){
      if (r.errCode == 12006) {
        that.setData({ needGps: true });
      }
      that.setData({ wifiList: [], selWifi: '请选择WiFi', wifiOpen:false});
		  wx.hideLoading();
	  }
    });
  },
  getWiFiList: function (){
	var that = this;
	//初始化wifi模块
    wx.startWifi({
      success: function (r) {
    	//获取列表
		  wx.onGetWifiList(function (res) {
			  wx.hideLoading();
			  that.setData({wifiList:res.wifiList,showPic: false});
		  });
      },
      fail: function(){
    	  that.setData({wifiOpen: false});
		  that.setData({wifiList:[],selWifi: '请选择WiFi'});
		  wx.hideLoading();
      }
    });
  },
  selWifi: function (e){
	  var wifiList = this.data.wifiList;
	  var idx = e.detail.value;
	  this.setData({selWifi: wifiList[idx].SSID, idx: idx});
  },
  confirmSubmit: function(e){
	  var that = this;
	  //编辑入驻信息
	  var wifi = that.data.wifi;
	  var wifiList = that.data.wifiList;
	  var idx = that.data.idx;
	  var isAdv = that.data.isAdv;
	  var data = {
			realname: e.detail.value.realname,
			mobile: e.detail.value.mobile,
			psw: e.detail.value.psw,
			stitle: e.detail.value.stitle,
			m: 'junsion_wifinav',
			op: 'addList',
			openid: that.data.openid
		  }
	  if(!data.realname){
		  wx.showModal({
			  title: '温馨提示',
			  content: '请输入姓名',
			  showCancel: false,
		  });
		  return;
	  }
	  if(!data.mobile){
		  wx.showModal({
			  title: '温馨提示',
			  content: '请输入电话',
			  showCancel: false,
		  });
		  return;
	  }
	  if(!data.stitle){
		  wx.showModal({
			  title: '温馨提示',
			  content: '请输入店名',
			  showCancel: false,
		  });
		  return;
	  }
	if(wifi){
		data.id = wifi.id;
		data.SSID = e.detail.value.SSID;
		if(!data.SSID){
		  wx.showModal({
	        title: '温馨提示',
	        content: '请输入WiFi名',
	        showCancel: false,
	      });
		}
	}else{
		if (!idx) {
			wx.showModal({
				title: '温馨提示',
				content: '请选择WiFi',
				showCancel: false,
			});
			return;
		}
		data.BSSID = wifiList[idx].BSSID;
		data.SSID = wifiList[idx].SSID;
		if(!wifiList[idx]){
			wx.showModal({
				title: '温馨提示',
				content: '请选择WiFi',
				showCancel: false,
			});
			return;
		}
	}
	  
	  //选则启用广告
	  data.isAdv = 0;
	  if(isAdv) data.isAdv = 1;
	  if(isAdv && that.data.uploadPic){
		  var advPic = that.data.advPic;
		  if(!advPic){
			  wx.showModal({
				  title: '温馨提示',
				  content: '请上传广告图',
				  showCancel: false,
			  });
			  return;
		  }
		  if(!wifi || !wifi.advPic){
			  if (!that.data.pkey){
			        wx.showModal({
			          title: '温馨提示',
			          content: '请选择套餐',
			          showCancel: false,
			        });
			        return;
			      }
			  var pid = that.data.packages[that.data.pkey].id;
			  if(!pid){
				  wx.showModal({
					  title: '温馨提示',
					  content: '请选择套餐',
					  showCancel: false,
				  });
				  return;
			  }
		  }
		  wx.showLoading({title: '上传中...'});
		  data.pid = pid;
		  data.m = 'junsion_wifinav';
		  wx.uploadFile({
		      url: app.util.url('entry/wxapp/upload'),
		      filePath: advPic,
		      name: 'photo',
		      header: { 'content-type': 'application/x-www-form-urlencoded' },
		      formData: data,
		      success: function (res) {
            wx.hideLoading();
            res['data'] = JSON.parse(app.delBoM(res.data));
		    	  if(res.data.code==1){
		    		  wx.requestPayment({
		    			  timeStamp: res.data.params.timeStamp,
		    			  nonceStr: res.data.params.nonceStr,
		    			  package: res.data.params.package,
		    			  signType: res.data.params.signType,
		    			  paySign: res.data.params.paySign,
		    			  success: function(res){
		    				  wx.showModal({
		    					  title: '温馨提示',
		    					  content: '支付成功',
		    					  showCancel: false,
		    					  success: function(r){
		    							wx.navigateTo({url: '../user/mylist/index'});
		    					  }
		    				  });
		    			  }
		    		  });
		    	  }else if(res.data.code == 10){
              wx.showModal({
                title: '温馨提示',
                content: '提交成功',
                showCancel: false,
                success: function (r) {
                  wx.navigateTo({ url: '../user/mylist/index' });
                }
              });
		    	  }else if(res.data.code == 11){
		    		  wx.showModal({
		    			  title: '温馨提示',
		    			  content: '保存成功',
		    			  showCancel: false,
		    			  success: function (r) {
		    				  wx.navigateTo({ url: '../user/mylist/index' });
		    			  }
		    		  });
            }else if(res.data.code == 2){
		    		  wx.showModal({
    					  title: '温馨提示',
    					  content: res.data.msg,
    					  showCancel: false,
    				  });
		    	  }else{
		    		  wx.showModal({
		    			  title: '温馨提示',
		    			  content: '系统错误',
		    			  showCancel: false,
		    		  });
		    	  }
		      }
		  });
		  return;
	  }
	  
    wx.showLoading({ title: '上传中...' });
	  app.util.request({
		url: 'entry/wxapp/enter',
		data: data,
		cachetime: 0,
		success: function (res) {
			wx.hideLoading();
			var data = res.data;
			if(data.code==1){
			  wx.showModal({
				  title: '温馨提示',
				  content: data.msg,
				  showCancel: false,
				  success: function(r){
					  if(r.confirm){
						  wx.navigateTo({url: '../user/mylist/index'});
					  }
				  }
			  });
			}else{
			  wx.showModal({
				  title: '温馨提示',
				  content: data.msg,
				  showCancel: false,
			  });
			}
		}
	});
  },
  onShareAppMessage: function () {
	  return {
	      title: this.data.cfg.share.enterShare,
	      path: '/sharing/pages/enter/enter',
	      imageUrl: this.data.cfg.share.enterPic,
	      success: function(res) {
	        // 转发成功
	      },
	      fail: function(res) {
	        // 转发失败
	      }
	    }
  },
  onPullDownRefresh: function(){
	  this.onLoad();
	  wx.stopPullDownRefresh();
  },
  imgView: function(e){
    wx.previewImage({
      urls: [e.target.dataset.src],
    })
  },
  switchChange: function(e){
	  this.setData({isAdv: e.detail.value});
  },
  clickPic: function(e){
	  var that = this;
	  wx.chooseImage({
		  count: 1,
		  success: function(res){
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success: function(img){
            that.setData({ uploadPic: true, advPic: res.tempFilePaths[0], advHeight: 652 / img.width * img.height});
          }
        })
			  
		  }
	  })
  },
  selPackage: function(e){
	  var idx = e.detail.value;
	  this.setData({pkey: idx});
  },
  onStaus: function(){
    var t = this.data.DOWN;
    if(t == 'slidedown') t = 'slideup';
    else t = 'slidedown';
    this.setData({DOWN:t});
  }
})